package com.yxy.patient.widiget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;


import com.yxy.patient.R;
import com.yxy.patient.utils.Views;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

public class TabIndicator extends MagicIndicator {
    private ViewPager2 viewPager2;
    private ViewPager viewPager;
    private FragmentContainerHelper fragmentContainerHelper;
    private boolean isEnableDivider = false;

    public TabIndicator(Context context) {
        super(context);
        init();
    }

    public TabIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        fragmentContainerHelper = new FragmentContainerHelper(this);
    }

    public void setViewPager2(ViewPager2 viewPager2) {
        this.viewPager2 = viewPager2;
        this.viewPager = null;
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        this.viewPager2 = null;
    }

    public void setCurrentItem(int index) {
        fragmentContainerHelper.handlePageSelected(index);
    }

    public void enableDivider(boolean isEnableDivider) {
        this.isEnableDivider = isEnableDivider;
    }

    public void setTitles(String[] titles) {
        if (getNavigator() == null) {
            CommonNavigator commonNavigator = new CommonNavigator(getContext());
            commonNavigator.setAdjustMode(true);
            commonNavigator.setAdapter(new CommonNavigatorAdapter() {
                @Override
                public int getCount() {
                    return titles == null ? 0 : titles.length;
                }

                @Override
                public IPagerTitleView getTitleView(Context context, int index) {
                    ScaleTransitionPagerTitleView colorTransitionPagerTitleView = new ScaleTransitionPagerTitleView(context);
                    colorTransitionPagerTitleView.setText(titles[index]);
                    colorTransitionPagerTitleView.setOnClickListener(v -> {
                        if (viewPager2 != null) {
                            viewPager2.setCurrentItem(index);
                        }
                        if (viewPager != null) {
                            viewPager.setCurrentItem(index);
                        }
                        fragmentContainerHelper.handlePageSelected(index);
                    });
                    colorTransitionPagerTitleView.setSelectedColor(Views.getColor(R.color.black));
                    colorTransitionPagerTitleView.setNormalColor(Views.getColor(R.color.divider_grey));
                    colorTransitionPagerTitleView.setPadding(0, 0, 0, 0);
                    colorTransitionPagerTitleView.setTextSize(16);
                    return colorTransitionPagerTitleView;
                }

                @Override
                public IPagerIndicator getIndicator(Context context) {
                    LinePagerIndicator indicator = new LinePagerIndicator(context);
                    indicator.setColors(context.getResources().getColor(R.color.main_blue));
                    indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                    return indicator;
                }
            });
            setNavigator(commonNavigator);
            if (isEnableDivider) {
                LinearLayout titleContainer = commonNavigator.getTitleContainer();
                titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
                titleContainer.setDividerPadding(Views.dip2px(15));
                titleContainer.setDividerDrawable(new ColorDrawable(R.color.divider_grey) {
                    @Override
                    public int getIntrinsicWidth() {
                        return Views.dip2px(1);
                    }
                });
            }
        }
    }
}
